package ydn.edu.hqu.cst.word;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.content.SharedPreferences;

public class MainActivity extends AppCompatActivity {
    Button button;
    EditText editText;
    //读取数据
    private SharedPreferences sharedPreferences;
    //写入数据
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //用户名输入文本
        editText =  findViewById(R.id.main_et_username);
        //登陆按钮
        button =  findViewById(R.id.main_btn);
        //实例化sharedPreferences
        sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        //实例化editor
        editor = sharedPreferences.edit();
        //如果用户已经注册过了，则直接进入主界面
        if(sharedPreferences.getString("user","").isEmpty() == false){
            Intent intent=new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(MainActivity.this, FirstActivity.class);
            startActivity(intent);
        }
        //用户第一次使用app则需要，注册输入用户名
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //putString写入数据：键值对，参数：key,value
                editor.putString("user",editText.getText().toString());
                editor.apply();
                Intent intent=new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(MainActivity.this, FirstActivity.class);
                startActivity(intent);
            }
        });
    }
}
